<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose
    :title="title"
    :showCancelBtn="true"
    :showOkBtn="true"
    :canFullscreen="false"
    :centered="true"
    :width="750"
    wrapClassName="policy"
    @ok="handleOk"
  >
    <BasicForm @register="registerForm" :model="modelRef" />
  </BasicModal>
</template>

<script lang="ts" setup>
import { ref, computed, unref } from 'vue';
import { BasicModal, useModalInner } from '/@/components/Modal';
import { BasicForm, useForm } from '/@/components/Form/index';
import { formSchema } from '../Policy.data';
import { one, saveOrUpdate } from '../Policy.api';
import { customerSelect } from '/@/views/good/product/Product.api';
// Emits声明
const emit = defineEmits(['register', 'success']);
const isUpdate = ref(true);
const customerOptions = ref([]);
//表单所有值，外部可更新
const modelRef = ref<any>({});

//设置标题
const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

type RangeValue = [string, string];

//表单配置
const [registerForm, { resetFields, setFieldsValue, validate, updateSchema, clearValidate }] = useForm({
  labelWidth: 100,
  schemas: formSchema,
  showActionButtonGroup: false,
});

//表单赋值
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //重置表单
  await resetFields();
  setModalProps({ confirmLoading: false, showCancelBtn: data?.showFooter, showOkBtn: data?.showFooter });
  isUpdate.value = !!data?.isUpdate;
  modelRef.value = {};
  customerOptions.value = await customerSelect()

  updateSchema([
    {
      field: "cId",
      componentProps: { options: customerOptions },
    },
    {
      field: "effectDate",
      componentProps: { 
        onChange: onChange,
      },
    },
  ])

  if (unref(isUpdate)) {
    //2022-05-30 覃东梁 修改
    const policy = await one({id: data.record.id})

    data.record = policy
    modelRef.value = policy;

    const customerPath = data.record.customerPath && data.record.customerPath.split(",")
    const arrays = customerPath <= 0 ? [] : customerPath.filter(item => item) //[366,381,382]//e
    data.record.cId = arrays;

    data.record.effectDate = [policy.effectSdate, policy.effectEdate];
  }

  // 无论新增还是编辑，都可以设置表单值
  if (typeof data.record === 'object') {
    setFieldsValue({
      ...data.record,
    });
  }

  await clearValidate();
});


const onChange = (val: RangeValue) => {
  var sDate = '', eDate = '';
  if (val && val != null && val.length > 1) {
    sDate = val[0];
    eDate = val[1];
  } 

  modelRef.value = {
    effectSdate: sDate,
    effectEdate: eDate
  }
};


//表单提交事件
async function handleOk() {
  try {
    let values = await validate()
    setModalProps({ confirmLoading: true });
    //提交表单
    await saveOrUpdate(values, isUpdate.value);
    //关闭弹窗
    closeModal();
    //刷新列表
    emit('success', { isUpdate: isUpdate.value, values });
  } catch (error) {
    console.warn(error);
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>

<style lang="less" scoped>
</style>